package org.yray.sip.base;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/***
 * 这个是消息处理类型，并不代表错误类型
 */
public enum  MsgType {





    REGISTER("REGISTER", "注册"),
    UNAUTHORIZED("Unauthorized", "未认证"),
    FORBIDDEN("Forbidden","禁止"),
    INVITE("INVITE", "外呼邀请"),
    OK("200 OK", "OK"),
    ACK("ACK", "ACK"),
    TRYING("Trying", "Trying"),
    CANCEL("CANCEL", "CANCEL"),
    NOTIFY("NOTIFY", "NOTIFY"),
    PROGRESS183("183", "SIP/2.0 183 Session Progress"),
    RINGING180("180", "SIP/2.0 180 Ringing"),
    BYE("BYE", "BYE"),


    BAD_400("400 Bad", "400 Bad Request"),
    NOT_FOUND_404("404", "404 Not Found"),
    REQUEST_TIMEOUT_408("408", "408 Request Timeout"),
    UNAVAILABLE_480("480", "480 Temporarily Unavailable"),
    TRANSACTION_NOT_481("481", "Call/Transaction Does Not Exist"),
    BUSY_486("486", "486 Busy Here"),
    REQUEST_TERMINATED_487("487", "487 Request Terminated"),
    NOT_ACCEPTABLE_488("488", "488 Not Acceptable Here"),
    INTERNAL_SERVER_ERROR_500("500", "500 Internal Server Error"),
    SERVICE_UNAVAILABLE_503("503", "503 Service Unavailable"),
    DECLINE_603("603", "603 Decline"),

    INFO("INFO", "INFO"),
    E5XX("SIP/2.0 50", "SIP/2.0 5XX"),
    E4XX("SIP/2.0 40", "SIP/2.0 4XX"),
    BLANK("BLANK", "BLANK"),
    UNKNOW("UNKNOW", "未知"),
    ;

    private  static Map<String,MsgType> preciseMap=new HashMap<>();
    private  static Map<String,MsgType> vagueMap=new HashMap<>();
    static {
        preciseMap.put(REGISTER.getCode(),REGISTER);
        preciseMap.put(UNAUTHORIZED.getCode(),UNAUTHORIZED);
        preciseMap.put(FORBIDDEN.getCode(),FORBIDDEN);
        preciseMap.put(INVITE.getCode(),INVITE);
        preciseMap.put(OK.getCode(),OK);
        preciseMap.put(ACK.getCode(),ACK);
        preciseMap.put(TRYING.getCode(),TRYING);
        preciseMap.put(CANCEL.getCode(),CANCEL);
        preciseMap.put(PROGRESS183.getCode(),PROGRESS183);
        preciseMap.put(RINGING180.getCode(),RINGING180);
        preciseMap.put(BYE.getCode(),BYE);
        preciseMap.put(NOTIFY.getCode(),NOTIFY);


        preciseMap.put(BAD_400.getCode(),BAD_400);
        preciseMap.put(NOT_FOUND_404.getCode(),NOT_FOUND_404);
        preciseMap.put(REQUEST_TIMEOUT_408.getCode(),REQUEST_TIMEOUT_408);
        preciseMap.put(UNAVAILABLE_480.getCode(),UNAVAILABLE_480);
        preciseMap.put(TRANSACTION_NOT_481.getCode(),TRANSACTION_NOT_481);
        preciseMap.put(BUSY_486.getCode(),BUSY_486);
        preciseMap.put(REQUEST_TERMINATED_487.getCode(),REQUEST_TERMINATED_487);
        preciseMap.put(NOT_ACCEPTABLE_488.getCode(),NOT_ACCEPTABLE_488);
        preciseMap.put(INTERNAL_SERVER_ERROR_500.getCode(),INTERNAL_SERVER_ERROR_500);
        preciseMap.put(SERVICE_UNAVAILABLE_503.getCode(),SERVICE_UNAVAILABLE_503);
        preciseMap.put(DECLINE_603.getCode(),DECLINE_603);

        vagueMap.put(E5XX.getCode(),E5XX);
        vagueMap.put(E4XX.getCode(),E4XX);
    }

    private  String code;
    private String displayName;
    MsgType(String code , String displayName){
        this.code = code;
        this.displayName = displayName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDisplayName() {
        return displayName;
    }

    public static MsgType getMsgType(String firstRow)
    {
        if(StringUtils.isEmpty(firstRow)) {
            return MsgType.BLANK;
        }
        //先查精确的
        for(Map.Entry<String,MsgType> item:preciseMap.entrySet())
        {
            if(firstRow.indexOf(item.getKey())>-1)
            {
                return item.getValue();
            }
        }
        //查模糊的
        for(Map.Entry<String,MsgType> item:vagueMap.entrySet())
        {
            if(firstRow.indexOf(item.getKey())>-1)
            {
                return item.getValue();
            }
        }
        return MsgType.UNKNOW;
    }
}
